package com.google.code.lf.gfm.ui;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.application.IWorkbenchConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.statushandlers.AbstractStatusHandler;
import org.eclipse.ui.statushandlers.StatusManager;

import com.google.code.lf.gfm.ui.error.WorkbenchErrorHandler;
import com.google.code.lf.gfm.ui.log.Console;
import com.google.code.lf.gfm.ui.log.LogHelper;

//TODO javadoc
public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {

	public static final String PERSPECTIVE_ID = "com.google.code.lf.gfm.ui.perspective";

	private final WorkbenchErrorHandler workbenchErrorHandler = new WorkbenchErrorHandler();

	@Override
	public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(
			final IWorkbenchWindowConfigurer configurer) {
		return new ApplicationWorkbenchWindowAdvisor(configurer);
	}

	@Override
	public void initialize(final IWorkbenchConfigurer configurer) {
		super.initialize(configurer);
		configurer.setSaveAndRestore(true);
	}

	@Override
	public void preStartup() {
		// Ajout de la console spécifique à la vue console standard eclipse
		ConsolePlugin.getDefault().getConsoleManager().addConsoles( new IConsole[] { new Console().getMessageConsole() } );

		// Configuration du logger
		LogHelper.configure();

		super.preStartup();
	}

	@Override
	public String getInitialWindowPerspectiveId() {
		return PERSPECTIVE_ID;
	}

	@Override
	public void postStartup() {
		super.postStartup();
		LogHelper.info("Application GrisbiFileManagerUI démarrée.");
	}

	@Override
	public void eventLoopException(final Throwable exception) {
		final IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, exception.getMessage(), exception);
		workbenchErrorHandler.handle(status, StatusManager.SHOW);
	}

	@Override
	public AbstractStatusHandler getWorkbenchErrorHandler() {
		return workbenchErrorHandler;
	}

}
